﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using Compiled_Tests.AutomationAbstractions;

using Tedds_Tool.Instructions;
using CommonInterfaces.Models;
using StandardScriptItems.SimpleControls;
using LEET.CommonInterfaces.Factories;
using LEET.CommonInterfaces.Automation;
using LEET.LEETCommon.AutomationAbstractions;
using LEET.CommonInterfaces.Models;
using LEET.LEETCommon.Model;
using System.Windows;
using LEET.StandardModuals.Instructions.Actions;

namespace Tedds_Tool.Automatables
{
    public class Invokable : ILEETActionModual
    {

        public AutomationElementEventListener CreateListener(IObservableWidget elem, IRegistryAccessor reg)
        {
            return new EventListener(elem, reg);
        }

        public bool CanCompile(string instruction)
        {
            try
            {
                String[] parts = Instruction.ParseInstruction(instruction);
                return "ACTION".Equals(parts[0]) && TypeName.Equals(parts[1]);
            }
            catch (InvalidInstructionException)
            {
                return false;
            }
        }

        public IInstruction CreateInstruction(string instruction)
        {
            return new Invoke(Instruction.ParseInstruction(instruction));
        }

        

        private class EventListener : AutomationElementEventListenerBase
        {

            AutomationEventHandler handler;
            private IRegistryAccessor registry;

            public EventListener(IObservableWidget target, IRegistryAccessor reg)
            {
                this.RootTarget = target;
                this.registry = reg;
                Listening = false;
                try
                {
                    handler = new AutomationEventHandler(CatchEvent);
                    registry.RegisterEventHandler(InvokePattern.InvokedEvent, RootTarget.ae, handler);
                    Listening = true;
                }
                catch (ArgumentException) {  }
            }

            public override void Listen(IObservableWidget widget)
            {
                //In case element disappeared before it could be processed
                if (widget == null) return;

                string element = widget.GetUIAHierarchy(RootTarget).ToString();
                Delagate(new Event_Pair("ACTION\t| INVOKE | " + element));
                
            }

            public override void Dispose()
            {
                registry.RemoveEventHandler(InvokePattern.InvokedEvent, RootTarget.ae, handler);
                base.Dispose();
            }

        }

        public void Initialize()
        {
        }

        public string TypeName
        {
            get { return "INVOKE"; }
        }

        public System.Windows.UIElement CreateScriptItemDetails()
        {
            throw new NotImplementedException();
        }


        public FrameworkElement CreateScriptItemDetails(LEET.CommonInterfaces.Nil onUpdate, IEnumerable<string> processes, IElementState state)
        {
            return new ElementStateComboBox(state);
        }
    }
}
